package com.nb.provider;



import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Objects;

/**
 * 响应对象
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;


    public static <T> Result<T> ok() {
        return result(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),null);
    }


    public static <T> Result<T> ok(T data) {
        return result(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),data);
    }


    public static <T> Result<T> error() {
        return result(ResultEnum.FAIL.getCode(),ResultEnum.FAIL.getMsg(),null);
    }


    public static <T> Result<T> error(int code,String msg) {
        return result(code,msg,null);
    }

    public static <T> Result<T> error(ResultEnum resultEnum) {
        return result(resultEnum.getCode(),resultEnum.getMsg(),null);
    }

    public static <T> Result<T> error(String msg) {
        return result(ResultEnum.FAIL.getCode(),msg,null);
    }

    private static <T> Result<T> result(int code,String msg,T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        if(Objects.nonNull(data)){
            result.setData(data);
        }
        return result;
    }


}
